package cn.yunhe.util;

import cn.yunhe.entity.*;
import cn.yunhe.service.impl.DepartmentServiceImpl;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


/**
 * List 转 User
 *
 * @Author hu
 */
@Component
public class ListToUserUtil {

    @Autowired
    protected DepartmentServiceImpl departmentService;
    private static ListToUserUtil listToUserUtil;


    @PostConstruct
    public void init() {
        listToUserUtil = this;
        listToUserUtil.departmentService = this.departmentService;
    }

    public User getUser(List<String> list, HttpSession session) {
        User user = new User();
        int i = 1;
        String username = "";
        for (String str : list) {
            switch (i) {
                case 1:
                    user.setUserName(str);
                    username = str;
                    break;
                case 2:
                    user.setTrueName(str);
                    break;
                case 3:
                    user.setNationalId(str);
                    break;
                case 4:
                    user.setPassword(new SimpleHash("MD5", str, username).toString());
                    break;
                case 5:
                    Department department = listToUserUtil.departmentService.getDepByName(str);
                    user.setDepartment(department);
                    break;
            }
            i++;
        }
        user.setCreateTime(new Date());
        User u = (User) session.getAttribute("Curr_User");
        user.setCreateBy(u.getUserId());
        user.setEnabled(1);
        user.setEmail("无");
        user.setPhoneNum("无");
        return user;
    }
}
